// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/base/api/DeviceDetailApi'

import OneClass from '@api/base/model/QueryDeviceDetailReq'
import AddClass from '@api/base/model/InsertDeviceReq'
import EditClass from '@api/base/model/UpdateDeviceReq'
import DelClass from '@api/base/model/DeleteDeviceReq'
import ClearClass from '@api/base/model/CleanRelationDeviceReq'
import BaseClass from '@api/base/model/DeviceDto'
import TagClass from '@api/base/model/DeviceLabelDto'

const CLIENT = CORE.grpcCLIENT(Client)
const TAG = [TagClass, 'deviceLabelDtos', 1]
const BASE = [BaseClass, 'content', null, (dto, obj) => CORE.grpcDTO(dto, obj, TAG)]

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne(one, TIP = '查询设备详情') {
    const DTO = CORE.grpcDTO(OneClass, { id: one.id })
    const resp = await CORE.grpc(TIP, CLIENT, 'queryDetailDevice', DTO)
    return (resp.content || [])[0] || one
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async add(one, TIP = '新增设备') {
    const DTO = CORE.grpcDTO(AddClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'insertDevice', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改设备') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'updateDevice', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '移除设备') {
    const DTO = CORE.grpcDTO(DelClass, { ids: id })
    await CORE.grpc(TIP, CLIENT, 'deleteDevice', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 清除 —————————————————————————————— //

  async clear({ id }, TIP = '清除关联设备') {
    const DTO = CORE.grpcDTO(ClearClass, { id })
    await CORE.grpc(TIP, CLIENT, 'cleanRelationDevice', DTO)
    return CORE.grpcOK(TIP)
  },
}
